package com.简单项目.学生管理系统;

import java.util.Scanner;
import java.util.Vector;

public class Student {
    int id;
    String name;
    static Vector<Student> vector = new Vector<>();
    static Scanner sc = new Scanner(System.in);

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    static void add() {
        System.out.print("id = ");
        int id = sc.nextInt();
        if(ID_is_repetition(id)){
            System.out.println("id 重复，添加不成功~");
            return;
        }
        System.out.print("name = ");
        String name = sc.next();
        Student student = new Student(id, name);
        vector.add(student);
        System.out.println("创建成功~~");
    }

    static void delete() {
        if (Vector_is_Empty()) {
            return;
        }
        System.out.println("您要删除序号为多少的信息？");
        int num = sc.nextInt();
        if (num < 0 || num >= vector.size()) {
            System.out.println("序号不在范围   删除失败~~");
            return;
        }
        vector.remove(num);
        System.out.println("删除成功~~");
    }

    static void update() {
        if (Vector_is_Empty()) {
            return;
        }
        System.out.println("您要更新序号为多少的信息？");
        int num = sc.nextInt();
        if (num < 0 || num >= vector.size()) {
            System.out.println("序号不在范围   更新失败~~");
            return;
        }
        Student student = vector.get(num);
        System.out.print("id = ");
        int id = sc.nextInt();
        System.out.print("name = ");
        String name = sc.next();
        student.id = id;
        student.name = name;
    }

    static void find() {
        if (Vector_is_Empty()) {
            return;
        }
        System.out.println("您要通过id 还是 name查找 （id/name）？");
        String choice = sc.next();
        if (choice.equals("id")) {
            System.out.println("请输入要查找的id");
            int id = sc.nextInt();
            for (int i = 0; i < vector.size(); i++) {
                Student student = vector.get(i);
                if (student.id == id) {
                    System.out.println(student);
                    return;
                }
            }
        } else if (choice.equals("name")) {
            System.out.println("请输入要查找的姓名");
            String name = sc.next();
            for (int i = 0; i < vector.size(); i++) {
                Student student = vector.get(i);
                if (student.name.equals(name)) {
                    System.out.println(student);
                    return;
                }
            }
        } else System.out.println("查找方式不正确");

        System.out.println("未找到该信息~~");

    }

    @Override
    public String toString() {
        return "id = " + id + "  name = " + name;
    }


    static boolean ID_is_repetition(int id) {
        for (int i = 0; i < vector.size(); i++) {
            Student student = vector.get(i);
            if (student.id == id)
                return true;
        }
        return false;
    }

    static boolean Vector_is_Empty(){
        if (vector.isEmpty()){
            System.out.println("无学生信息，不能操作");
            return true;
        }
        return false;
    }
}
